#region Using directives
using System;
using System.IO;
#endregion

namespace SharpWind.Helpers
{
	public class Log
	{
		#region Fields

		private StreamWriter streamWriter = null;

		#endregion

		#region Methods
		
		public Log( string name )
		{
			if( string.IsNullOrEmpty( name ) )
			{
				throw new ArgumentNullException( "name" );
			}

			FileStream fileStream = new FileStream( Path.Combine( Directories.Log, name ), FileMode.Create, FileAccess.Write, FileShare.Read );

			this.streamWriter = new StreamWriter( fileStream );
			this.streamWriter.AutoFlush = true;
			this.streamWriter.WriteLine( "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" );
			this.streamWriter.WriteLine( "=                              SharpWind Log System                               =" );
			this.streamWriter.WriteLine( "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" );
			this.streamWriter.WriteLine( "Session started at: " + DateTime.Now.ToString() );
			this.streamWriter.WriteLine( "Version: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() );
			this.streamWriter.WriteLine( "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" );
		}

		#endregion

		#region Methods

		public void WriteInfo( string system, string message )
		{
			Write( "Info", system, message );
		}

		public void WriteWarning( string system, string message )
		{
			Write( "Warning", system, message );
		}

		public void WriteError( string system, string message )
		{
			Write( "Error", system, message );
		}

		private void Write( string kind, string system, string message )
		{
			if( this.streamWriter != null )
			{
				DateTime t = DateTime.Now;
				string s = string.Format( "{0}:{1}:{2}|{3}|{4}|{5}",
										  t.Hour.ToString( "00" ),
										  t.Minute.ToString( "00" ),
										  t.Second.ToString( "00" ),
										  kind, system, message );

				this.streamWriter.WriteLine( s );
			}
		}

		#endregion
	}
}
